package org.example.controller;

import com.google.protobuf.Api;
import lombok.extern.slf4j.Slf4j;
import org.example.dao.activityDao;
import org.example.vo.ActivityVO;
import org.example.vo.ApiResponse;
import org.example.vo.tempActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/12/15
 */
@Slf4j
@Controller
public class ActivityController {
    @Autowired
    private activityDao activityDao;
    @GetMapping("/create")
    public String createActivity(){
        log.info("to create activity page.");
        return "activities/add-activities";
    }
    @PostMapping("/doCreate")
    @ResponseBody
    public ApiResponse createActivity(@RequestBody tempActivityVO temp){
        log.info("create activity.");
        activityDao.createActivity(temp);//创建活动
        return ApiResponse.ok("活动创建成功");
    }

    @GetMapping("/query")
    public String queryAllActivities(Model model){
        log.info("to query activities page.");
        ActivityVO[] activityVOS= activityDao.getAllActivity();
        log.info(Arrays.toString(activityVOS));
        model.addAttribute("acts",activityVOS);
        return "activities/query-activities";
    }

    @PostMapping("/doDelete/{id}")
    public String deleteActivities(@PathVariable("id")int id,Model model){
        log.info("delete{}",id);
        activityDao.deleteActivity(id);
        ActivityVO[] activityVOS= activityDao.getAllActivity();
        log.info(Arrays.toString(activityVOS));
        model.addAttribute("acts",activityVOS);
        return "activities/query-activities";
    }
}
